
import scriptedsands.ElementEngine;
import scriptedsands.Element;

public class DefaultEngine extends ElementEngine {
// variable/method declarations in parent class
//    public BaseParticle[][] sandbox;
//    public BaseParticle currentElem;
//    public VolatileImage vImg;
//    BufferedImage bImg = new BufferedImage(512, 512, BufferedImage.TYPE_INT_RGB);
//    public boolean isPaused;
//    public boolean isDrawing;
//    public long fps;

//    public boolean withinSandboxBounds(int x, int y) {
//        if (x >= 0 && y >= 0 && x < sandbox.length && y < sandbox[0].length)
//            return true;
//        return false;
//    }

    public DefaultEngine() {
        currentElem = Element.SAND;
    }

    public void draw() {
        if (isDrawing) {
            drawLineFilled(currentElem);
        }
    }

    public void putElement(Element elem, int x, int y) {
        if (withinSandboxBounds(x, y)) {
            drawElement(elem, x, y);
            sandbox[x][y] = elem;
        }
    }

    public void putCurrentElement(int x, int y) {
        putElement(currentElem, x, y);
    }

    public void moveElement(int x1, int y1, int x2, int y2) {
        if (withinSandboxBounds(x1,y1) && withinSandboxBounds(x2,y2)) {
            putElement(sandbox[x1][y1], x2, y2);
            putElement(Particle.EMPTY, x1,y1);
        }
    }

    public void clearSandbox() {
    }

    public void setElement(Object e) {
        currentElem = (Element) e;
    }

    ////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////

    public void fall() {
        for (int x = 0; x < sandbox.length; x++) {
            for (int y = 0; y < sandbox[x].length; y++) {
                if (sandbox[x][y] != Particle.EMPTY) {
                    int yg = sandbox[x][y].gravity + y;
                    if (withinSandboxBounds(x,yg) && sandbox[x][yg] == Particle.EMPTY) {
                        moveElement(x,y, x,yg);
                    }
                }
            }
        }
    }

    public void step() {
        fall();
    }
}